/*
 * @Author: your name
 * @Date: 2020-10-25 20:56:19
 * @LastEditTime: 2020-10-28 09:02:35
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \就业班练习d:\桌面\ma-pc\src\utils\request.js
 */
import axios from 'axios'
import { getUserInfo } from '@/store'
import JSONbig from 'json-bigint'

// 使用create方法生成一个全新的axios 实例
const instance = axios.create(
  {
    baseURL: 'http://ttapi.research.itcast.cn',
    transformResponse: [function (data) {
      // 处理返回数据 数据内可放多个处理函数
      try {
        return JSONbig.parse(data)
      } catch (err) {
        console.log('JSONbig转换错误')
        return data
      }
    }]
  }
)
// http request拦截器 添加一个请求拦截器
instance.interceptors.request.use(function (config) {
  const token = getUserInfo()
  if (token && token.token) {
    config.headers.Authorization = `Bearer ${token.token}`
  }
  return config
}, function (error) {
  return Promise.reject(error)
})

export default instance
